﻿Imports System.IO

Partial Class Page_PrintLabel_ExportFile
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Request("f") Is Nothing AndAlso Request("f") <> "" Then
            exportFile(Request("f"))
        End If
    End Sub

    Private Sub exportFile(ByVal FileName As String)
        FileName = Server.MapPath("~") & "\viewerTemp\" & FileName
        Try
            Dim strFileFullName As String = ""
            Dim strFileExt As String = ""
            Dim strFileContentType As String = ""
            Dim objFileInfor As FileInfo
            If File.Exists(FileName) Then
                objFileInfor = New FileInfo(FileName)
                If objFileInfor.Exists Then

                    strFileExt = Replace(LCase(objFileInfor.Extension), ".", "")
                    Select Case strFileExt
                        Case "pdf"
                            strFileContentType = "application/pdf"
                        Case "doc", "docx"
                            strFileContentType = "application/msword"
                        Case Else
                            strFileContentType = "application/x-application"
                    End Select

                    ' Clear the current output content from the buffer
                    Response.Clear()

                    ' Add the header that specifies the default filename 
                    ' for the Download/SaveAs dialog
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & objFileInfor.Name)

                    ' Add the header that specifies the file size, so that the 
                    ' browser can show the download progress
                    Response.AddHeader("Content-Length", objFileInfor.Length.ToString())

                    ' Specify that the response is a stream that cannot be read _
                    ' by the client and must be downloaded
                    Response.ContentType = strFileContentType

                    ' Send the file stream to the client
                    Response.WriteFile(objFileInfor.FullName)

                    ' Release objects
                    objFileInfor = Nothing
                    'tblTemp = Nothing

                    ' Stop the execution of this page
                    Response.End()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub


End Class
